<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增任务 </a-button>
      </template>
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.key === 'roleName'">
          <Tooltip placement="top">
            <template #title>
             {{record.remark}}
            </template>
            <span>
             {{text}}
            </span>
          </Tooltip>
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
               {
                icon: 'ant-design:file-protect-outlined',
                tooltip: '查看执行记录',
                color: 'success',
                onClick: handleView.bind(null, record),
              },
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <TaskDrawer @register="registerDrawer" @success="handleSuccess" />
    <TaskLogDrawer @register="registerTaskLogDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { Tooltip } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getRoleListByPage } from '@/api/demo/system';

  import { useDrawer } from '@/components/Drawer';
  import TaskDrawer from './TaskDrawer.vue';
  import TaskLogDrawer from './TaskLogDrawer.vue';
  import { columns, searchFormSchema } from './task.data';
  defineOptions({ name: 'TaskManagement' });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTaskLogDrawer, { openDrawer: openTaskLogDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    title: '任务调度',
    api: getRoleListByPage,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    handleSearchInfoFn(info) {
      console.log('handleSearchInfoFn', info);
      return info;
    },
    bordered: true,
    useSearchForm: true,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: false,

    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action'
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    console.log(record);
  }

  function handleView(record: Recordable) {
    openTaskLogDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  function handleSuccess() {
    reload();
  }
</script>
